using AdminAuthority;
using AdminAuthority.Attributes;

using Abc.Application;
using Abc.Core;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

using Tool;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers
{
    /// <summary>
    /// 开发
    /// </summary>
    //[ValidateModelFilter]
    [AuthorityController(typeCode: "HtAdmin", controllerDescription: "开发")]
    public class DeveloperController : HtAdminBaseController
    {
        private readonly ILogger<MenuController> _logger;
        private readonly IDeveloperService _developerService;
        private readonly ICacheService _cacheService;
        private readonly IPermissionService _permissionService;

        public DeveloperController(ILogger<MenuController> logger
        , IDeveloperService developerService
        , ICacheService cacheService
        , IPermissionService permissionService)
        {
            _logger = logger;
            _developerService = developerService;
            _cacheService = cacheService;
            _permissionService = permissionService;
        }

        [AuthorityPermission(name: "浏览", actions: "Index")]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 更新权限数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AuthorityPermission(name: "更新权限数据", actions: "UpdateMenuData")]
        public async Task<JsonResult> UpdateMenuData()
        {
            var resultdata = await _permissionService.UpdatePermission();
            return Json(resultdata);
        }

        /// <summary>
        /// 代码表单生成
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AuthorityPermission(name: "代码表单生成", actions: "CodeForm")]
        public async Task<IActionResult> CodeForm()
        {
            return View();
        }

        /// <summary>
        /// 刷新缓存
        /// </summary>
        /// <returns></returns>
        [AuthorityPermission(name: "刷新缓存", actions: "RefreshCache")]
        public async Task<JsonResult> RefreshCache()
        {
            var resultdata = await _cacheService.RefreshCache();
            return Json(resultdata);
        }

        [HttpGet]
        public ActionResult TestAf()
        {
            //var type = typeof(CustomAttributes);

            //var attribute = type.GetCustomAttributes(typeof(AuthorityFunctionAttribute), false).FirstOrDefault();

            //var types = GetType().Assembly.GetTypes().Where(t => (t.IsClass || t.IsGenericMethodParameter) && t.GetCustomAttributes(typeof(AuthorityFunctionAttribute), false).Length > 0);

            var m1 = AuthorityHelper.GetMvcPermissions();

            var m2 = AuthorityHelper.GetApiPermissions();
            return View();
        }

        public IActionResult TestValidate()
        {
            return View();
        }

        [HttpPost]
        public ActionResult TestValidate(TestValidateDto input)
        {
            return Json(new ResultData<string>() { Succeeded = true, Data = input.Name });
        }

        public class TestValidateDto
        {
            [Required(ErrorMessage = "名字必填"), MinLength(5, ErrorMessage = "名字最小长度5")]
            public string Name { get; set; }

            [Required(ErrorMessage = "标题必填"), MinLength(5, ErrorMessage = "标题最小长度5")]
            public string Title { get; set; }
        }
    }
}